//package com.codegeek.framework.interceptor;
//
//import java.lang.reflect.Method;
//import java.util.HashMap;
//import javax.servlet.http.Cookie;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import javax.servlet.http.HttpSession;
//
//import com.codegeek.common.utils.SecurityUtils;
//import com.codegeek.framework.web.domain.server.Sys;
//import com.codegeek.project.common.Decode;
//import org.springframework.stereotype.Component;
//import org.springframework.web.method.HandlerMethod;
//import org.springframework.web.servlet.ModelAndView;
//import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
//import com.alibaba.fastjson.JSONObject;
//import com.codegeek.common.utils.ServletUtils;
//import com.codegeek.framework.interceptor.annotation.RepeatSubmit;
//import com.codegeek.framework.web.domain.AjaxResult;
//
///**
// * 防止重复提交拦截器
// *
// * @author codegeek
// */
//@Component
//public abstract class RepeatSubmitInterceptor2 extends HandlerInterceptorAdapter
//{
//
//    private HashMap<String,String> sessionMap = new HashMap<>();
//
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
//    {
//        if (handler instanceof HandlerMethod)
//        {
//            HandlerMethod handlerMethod = (HandlerMethod) handler;
//            Method method = handlerMethod.getMethod();
//            RepeatSubmit annotation = method.getAnnotation(RepeatSubmit.class);
//            if(request.getRequestURI().indexOf("captchaImage")==-1) {
//
//                if(request.getRequestURI().indexOf("login")==-1) {
//
//                    if(request.getSession().getAttribute("userName")!=null){
//                        System.out.println("check:"+request.getSession().getAttribute("userName"));
//                    }else {
//                        System.out.println("session已更改！");
//                        AjaxResult ajaxResult = AjaxResult.error("没有权限，请联系管理员授权");
//                        ServletUtils.renderString(response, JSONObject.toJSONString(ajaxResult));
//                        return false;
//                    }
//
//                    String ip = getIPAddress(request);
//                    //ip拦截
////                    if(!ip.equals(SecurityUtils.getLoginUser().getIpaddr())){
////                        AjaxResult ajaxResult = AjaxResult.error("没有权限，请联系管理员授权");
////                        ServletUtils.renderString(response, JSONObject.toJSONString(ajaxResult));
////                        return false;
////                    }
//
////                    for(Cookie cookie:request.getCookies()){
//////                        if(cookie.getName().equals("JSESSIONID")){
////////                            System.out.println("jSessionId:"+cookie.getValue());
////////                            System.out.println("updateSessionId:"+sessionMap.get(SecurityUtils.getUsername()));
//////                            //jsSessionId拦截，完全模拟也无效
//////                            if(sessionMap.containsKey(SecurityUtils.getUsername())) {
//////                                if (!sessionMap.get(SecurityUtils.getUsername()).equals(cookie.getValue())) {
////////                                    System.out.println("sessionId已改变!");
//////                                    AjaxResult ajaxResult = AjaxResult.error("没有权限，请联系管理员授权");
//////                                    ServletUtils.renderString(response, JSONObject.toJSONString(ajaxResult));
//////                                    return false;
//////                                }
//////                            }
//////                            sessionMap.put(SecurityUtils.getUsername(),cookie.getValue());
//////                        }
//////                    }
//                }
//
////                if(request.getRequestURI().indexOf("login")>-1){
////                    String JSESSIONID = "";
////                    for(Cookie cookie:request.getCookies()){
////                        if(cookie.getName().equals("JSESSIONID")){
////                            JSESSIONID = cookie.getValue();
////                        }
////                    }
////                    sessionMap.put(Decode.getValue(request.getParameter("username")),JSESSIONID);
////                }
//            }
//
//
//            if (annotation != null)
//            {
//                if (this.isRepeatSubmit(request))
//                {
//                    AjaxResult ajaxResult = AjaxResult.error("不允许重复提交，请稍后再试");
//                    ServletUtils.renderString(response, JSONObject.toJSONString(ajaxResult));
//                    return false;
//                }
//            }
//            return true;
//        }
//        else
//        {
//            return super.preHandle(request, response, handler);
//        }
//    }
//
//    /**
//     * 验证是否重复提交由子类实现具体的防重复提交的规则
//     *
//     * @return
//     * @throws Exception
//     */
//    public abstract boolean isRepeatSubmit(HttpServletRequest request);
//
//
//    public static String getIPAddress(HttpServletRequest request) {
//        String ip = null;
//
//        //X-Forwarded-For：Squid 服务代理
//        String ipAddresses = request.getHeader("X-Forwarded-For");
//        if (ipAddresses == null || ipAddresses.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses)) {
//            //Proxy-Client-IP：apache 服务代理
//            ipAddresses = request.getHeader("Proxy-Client-IP");
//        }
//        if (ipAddresses == null || ipAddresses.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses)) {
//            //WL-Proxy-Client-IP：weblogic 服务代理
//            ipAddresses = request.getHeader("WL-Proxy-Client-IP");
//        }
//        if (ipAddresses == null || ipAddresses.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses)) {
//            //HTTP_CLIENT_IP：有些代理服务器
//            ipAddresses = request.getHeader("HTTP_CLIENT_IP");
//        }
//        if (ipAddresses == null || ipAddresses.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses)) {
//            //X-Real-IP：nginx服务代理
//            ipAddresses = request.getHeader("X-Real-IP");
//        }
//
//        //有些网络通过多层代理，那么获取到的ip就会有多个，一般都是通过逗号（,）分割开来，并且第一个ip为客户端的真实IP
//        if (ipAddresses != null && ipAddresses.length() != 0) {
//            ip = ipAddresses.split(",")[0];
//        }
//
//        //还是不能获取到，最后再通过request.getRemoteAddr();获取
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses)) {
//            ip = request.getRemoteAddr();
//        }
//        return ip.equals("0:0:0:0:0:0:0:1")?"127.0.0.1":ip;
//    }
//
//
//
//
//}
